package com.schedule.exception_advice;


import com.schedule.common.CommonResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {
     /**
     * 自定义业务异常拦截
     * BusinessException
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public CommonResult bussinessexception(BusinessException e) {

        return CommonResult.error(e.getCode(),e.getMessage(),e.getMessage());
    }

    /**
     * 未知的运行时异常拦截
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public CommonResult notFount(RuntimeException e) {
        return CommonResult.error(BusinessExceptionEnum.SERVER_ERROR.getCode(),e.getMessage(),
                BusinessExceptionEnum.SERVER_ERROR.getMessage());
    }
}